import datetime

from django.db.models import Sum
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from base.models import Unit, StoreHouse, Category, CustSuppClass, CustomSupplier, Goods, Company, AccountTitle, \
    Employee


class UnitSerializer(ModelSerializer):
    """
    计量单位
    """
    class Meta:
        model = Unit
        exclude = ["update_time", "add_time"]


class StoreHouseSerializer(ModelSerializer):
    """
    仓库表
    """
    flag_name = serializers.CharField(source='get_flag_display', read_only=True)

    class Meta:
        model = StoreHouse
        exclude = ["update_time", "add_time"]


class CategorySerializer(ModelSerializer):
    """
    商品类别
    """
    children = serializers.SerializerMethodField(read_only=True)
    parent_name = serializers.CharField(source='parent.name', read_only=True)

    class Meta:
        model = Category
        exclude = ["update_time", "add_time"]

    def get_children(self, obj):
        if obj.children.count() == 0:
            return None
        return self.__class__(obj.children.all(), many=True).data


class CustSuppClassSerializer(ModelSerializer):
    """
    客商分类
    """
    parent_name = serializers.CharField(source='parent.name', read_only=True)
    children = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = CustSuppClass
        exclude = ["update_time", "add_time"]

    def get_children(self, obj):
        if obj.children.count() == 0:
            return None
        return self.__class__(obj.children.all(), many=True).data


class CustomSupplierSerializer(ModelSerializer):
    """
    客户供应商
    """
    cate_name = serializers.CharField(source='cate.name', read_only=True)
    user_name = serializers.CharField(source='user.real_name', read_only=True)
    vip_name = serializers.CharField(source='get_is_vip_display', read_only=True)
    customer_flag = serializers.SerializerMethodField()
    supplier_flag = serializers.SerializerMethodField()

    class Meta:
        model = CustomSupplier
        exclude = ["update_time", "add_time"]

    def get_customer_flag(self, obj):
        if obj.is_customer:
            return '是'
        return '否'

    def get_supplier_flag(self, obj):
        if obj.is_supplier:
            return '是'
        return '否'


class GoodsCodeSerializer(ModelSerializer):
    """
    商品编码
    """

    class Meta:
        model = Goods
        fields = ["id", "code", "name"]


class GoodsSerializer(ModelSerializer):
    """
    商品管理
    """
    cate_name = serializers.CharField(source='cate.name', read_only=True)
    unit_name = serializers.CharField(source='unit.name', read_only=True)
    supplier_name = serializers.CharField(source='supplier.name', read_only=True)
    user_name = serializers.CharField(source='user.real_name', read_only=True)
    stock_nums = serializers.SerializerMethodField()

    class Meta:
        model = Goods
        exclude = ["update_time", "add_time"]

    def get_stock_nums(self, obj):
        request = self.context["request"]

        if request and request.query_params.get("house_id", None):
            house_id = request.query_params.get("house_id")
            results = obj.stocks.filter(house_id=house_id).aggregate(goods_nums=Sum("quantity"))
        else:
            results = obj.stocks.all().aggregate(goods_nums=Sum("quantity"))

        if results:
            return results.get("goods_nums")
        return 0


class CompanySerializer(ModelSerializer):
    """
    公司信息
    """
    class Meta:
        model = Company
        exclude = ["update_time", "add_time"]


class AccountTitleSerializer(ModelSerializer):
    """会计科目"""
    children = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = AccountTitle
        fields = ['id', 'name',  'parent', 'code', 'desc', 'children']

    def get_children(self, obj):
        if obj.children.count() == 0:
            return ""
        return self.__class__(obj.children.all(), many=True).data


class AccountTitleListSerializer(ModelSerializer):
    """会计科目"""

    class Meta:
        model = AccountTitle
        fields = ['id', 'name',  'parent', 'desc']


class EmployeeSerializer(ModelSerializer):
    """员工信息"""

    class Meta:
        model = Employee
        exclude = ['update_at', 'create_at']
